<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../layui.css">
</head>
<body>        
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
</fieldset>

<form class="layui-form" action="/admin/user/save" method="post">

  <div class="layui-form-item">
    <div class ="layui-inline">
    <label class="layui-form-label">教师名称</label>
      <div class="layui-input-inline">
        <input type="text" name="tName" required  lay-verify="required" autocomplete="off" class="layui-input tName" disabled="disabled">
      </div>
    </div>
    <div class ="layui-inline">
    <label class="layui-form-label">教师工号</label>
      <div class="layui-input-inline">
        <input type="text" name="tSerial" required  lay-verify="required" autocomplete="off" class="layui-input tSerial"disabled="disabled">
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class = "layui-inline">
      <label class = "layui-form-label">联系方式</label>
      <div class= "layui-input-inline">
        <input type="text" name="telephone"  placeholder="请输入联系方式" autocomplete="off" class ="layui-input telephone">
      </div>
    </div>
    <div class = "layui-inline">
      <label class = "layui-form-label">课程名称</label>
      <div class= "layui-input-inline">
        <input type="text" name="courseName"   autocomplete="off" class ="layui-input courseName" disabled="disabled">
      </div>
    </div>
  </div>
  <div class = "layui-form-item">
    <div class = "layui-inline">
      <label class = "layui-form-label">申请编号</label>
      <div class= "layui-input-inline">
        <input type="text" name="applySerial" autocomplete="off" class ="layui-input courseSerial applySerial" disabled="disabled">
      </div>
    </div>
    <div class = "layui-inline">
      <label class="layui-form-label">班级选择</label>
      <div class="layui-input-inline">
        <select name="grade" lay-verify="" lay-search>
          <option value="自订自用" selected>自订自用</option>
          <option value="信息管理与信息系统">信息管理与信息系统</option>
          <option value="工业工程">工业工程</option>
        </select> 
      </div>
    </div>
  </div>
  <div class = "layui-form-item">
    <div class = "layui-inline">
      <label class="layui-form-label">教材1选择</label>
      <div class="layui-input-inline">
        <select name="name" id ="chooseMaterial" lay-verify="required" lay-search>
          <option value=""></option>
        </select> 
      </div>
    </div>
    <div class = "layui-inline">
      <label class = "layui-form-label">订购数量</label>
      <div class= "layui-input-inline">
        <input type="text" name="quantity"  placeholder="请输入数量" autocomplete="off" class ="layui-input" lay-verify="required">
      </div>
    </div>
  </div>
  <div class = "layui-form-item">
    <div class = "layui-inline">
      <label class="layui-form-label">教材2选择</label>
      <div class="layui-input-inline">
        <select name="secondName" id ="sChooseMaterial" lay-search>
          <option value=""></option>
        </select> 
      </div>
    </div>
    <div class = "layui-inline">
      <label class = "layui-form-label">订购数量</label>
      <div class= "layui-input-inline">
        <input type="text" name="sQuantity"  placeholder="请输入数量" autocomplete="off" class ="layui-input">
      </div>
    </div>
  </div>
  <div class = "layui-form-item">
    <div class = "layui-inline">
      <label class="layui-form-label">教材3选择</label>
      <div class="layui-input-inline">
        <select name="thirdName" id ="tChooseMaterial" lay-search>
          <option value=""></option>
        </select> 
      </div>
    </div>
    <div class = "layui-inline">
      <label class = "layui-form-label">订购数量</label>
      <div class= "layui-input-inline">
        <input type="text" name="tQuantity"  placeholder="请输入数量" autocomplete="off" class ="layui-input">
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
    </div>
  </div>
</form>

<script src="../layui.js" charset="utf-8"></script>

<script src="../jquery-3.1.1.min.js"></script>




<script>



//获取参数
function getQueryVariable(variable){
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
    }
    return(false);
}

layui.use(['form', 'layedit', 'laydate','layer'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,upload = layui.upload
  ,laydate = layui.laydate;

  var $ =layui.jquery;

  setMessage();

//填充课程信息
  function setMessage(){
  var a = getQueryVariable("id");
  console.log(a);
  $.ajax({
      url:"/admin/course/getById/"+a,
      type:"post",
      dataType:"json",
      data:{},
      success:function(result){
      var message = result.data;
      console.log(message);
      $(".tName").val(message.tname);
      $(".courseName").val(message.courseName);
      var messageName = message.tname;
      setUserMessage(messageName);
      setMaterialMessahe(messageName);
      },
      error:function(result){
        console.log("sb");
      }
    })
  };
//填充用户信息
  function setUserMessage(messageName){
    $.ajax({
      url:"/admin/user/byName/"+messageName,
      type:"post",
      dataType:"json",
      data:{},
      success:function(result){
        var data = result.data;
        $(".tSerial").val(data.tSerial);
        $(".telephone").val(data.telephone);
        var oDate = new Date();
        var serial = oDate.getFullYear()+""+oDate.getHours()+oDate.getMinutes()+oDate.getSeconds();
        $(".applySerial").val(serial);
      },
      error:function(result){
        console.log("sb");
      }
    })
  };

//获得用户申报的教材
  function setMaterialMessahe(messageName){
    $.ajax({
      url:"/admin/material/MaByTName",
      async:false,
      type:"post",
      dataType:"json",
      data:{"tName":messageName},
      success:function(result){
        var optionData = result.data;
        var optionText;
        console.log(optionData);
        for(var i =0;i<optionData.length;i++){

          optionText = optionText + "<option value="+optionData[i].name+">"+optionData[i].name+"</option>";
        }
        if(optionData.length===1){
          $("#chooseMaterial").append(optionText);
        }else if(optionData.length===2){
          $("#chooseMaterial").append(optionText);
          $("#sChooseMaterial").append(optionText);
        }else{
          $("#chooseMaterial").append(optionText);
          $("#sChooseMaterial").append(optionText);
          $("#tChooseMaterial").append(optionText);
        }

        form.render('select');
      },
      error:function(result){
        console.log("setMaterialMessahe()出错了");
      }
    })
  };

  //日期
  laydate.render({
    elem: '#date'
  });
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');
 
  //自定义验证规则

  //以后要留下的验证区域



  form.verify({
    title: function(value){
      if(value.length < 5){
        return '标题至少得5个字符啊';
      }
    }
    ,pass: [/(.+){6,12}$/, '密码必须6到12位']
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });

  
  //监听提交
  
  form.on('submit(formDemo)', function(data){
    var data = data.field;
    var temp = 0;
    //教材2选择问题
    if(!((data.secondName===""&&data.sQuantity==="")||(data.secondName!=""&&data.sQuantity!=""))){
      layer.confirm("请选择教材或输入数量");
    }else if(data.secondName===data.name){
      layer.confirm("请选择不同种类的教材");
    }else{
      temp = 1;
    }
    //教材3选择问题
    if(!((data.thirdName===""&&data.tQuantity==="")||(data.thirdName!=""&&data.tQuantity!=""))){
      temp = 0;
      layer.confirm("请选择教材或输入数量");
    }else if(data.thirdName===data.name||data.thirdName===data.secondName){
      temp = 0;
      layer.confirm("请选择不同种类的教材");
    }else{
      temp = 1;
    }
    if(temp ===1){
      saveDetials(data);
    }

/*    saveDetials(data.applySerial,data.grade,data.name,data.telephone,data.quantity,data.courseName,data.tName);
    saveApply(data.applySerial,data.tSerial);*/
    return false;
  });


});

function saveDetials(message){
  $.ajax({
    url:"/admin/detials/save",
    type:"post",
    dataType:"json",
    data:{
      "applySerial":message.applySerial,
      "grade":message.grade,
      "name":message.name,
      "telephone":message.telephone,
      "quantity":message.quantity,
      "courseName":message.courseName,
      "tName":message.tName,
      "secondName":message.secondName,
      "sQuantity":message.sQuantity,
      "thirdName":message.thirdName,
      "tQuantity":message.tQuantity
    },
    success:function(result){
      console.log(result.data);
      saveApply(message.applySerial,message.tSerial);
    },
    error:function(result){
      console.log("sb");
    }
  })
}

function saveApply(applySerial,tSerial){
    var dDate = new Date();
    var applyDate = dDate.getFullYear()+"-"+(dDate.getMonth()+1)+"-"+dDate.getDate();
    $.ajax({
    url:"/admin/apply/save",
    type:"post",
    dataType:"json",
    data:{
      "applySerial":applySerial,
      "tSerial":tSerial,
      "applyTime":applyDate
    },
    success:function(result){
      alert("征订成功");
      console.log(result.data);
    },
    error:function(result){
      console.log("sb");
    }
  })
}


</script>

</body>
</html>